package com.yazdankhah.utils;

import com.yazdankhah.application.Const;
import com.yazdankhah.beans.Verse;
import static com.yazdankhah.utils.GenUtil.showStringDoubleArray;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.log4j.Logger;

/*
 * This class contains all file utilities that this application needs.
 * @author Ahmad Yazdankhah
 * @version 1.0
 */
public class FileUtil {
  
  private static final Logger logger = Logger.getLogger(FileUtil.class);
  
  
// ...................... P R I V A T E   M E T H O D S ......................//
// ...................... P U B L I C   M E T H O D S ........................//

  /**
   * read the given file and put them in an array list line by line.
   *
   * @param inFN - the given file name
   *
   * @return the array list containing the lines
   */
  public static List<String> fileToArrayList(String inFN) {
    /*.................... T A S K   M E S S A G E .......................*/
    //System.out.format("Reading from file %s %n", inFN);
        /*....................................................................*/

    List<String> strArr = new ArrayList<>();
    BufferedReader reader;

    try {
      reader = new BufferedReader(new FileReader(new File(inFN)));

      while (reader.ready()) {
        String line = reader.readLine().trim();
        if (line.length() == 0) {
          continue; //empty line
        }
        strArr.add(line);
      } //while

      reader.close();

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
    }

    return strArr;

  } //method


  public static void testfileToArrayList() {

    String inFN = "testPattern.txt";
    List<String> strArr = fileToArrayList(inFN);

    System.out.println("Array = " + strArr);

  } //test method


  /**
   * read a given file and skip the comment lines and return it as an array list
   *
   * @param inFN        - the given file name
   * @param commentChar - the comment line starts with this char
   *
   * @return the array list of strings containing each line
   */
  public static List<String> fileToArrayListSkipCommentLines(
          String inFN, char commentChar) {
    List<String> tempArr = new ArrayList<>();
    BufferedReader reader;
    String line;

    try {
      reader = new BufferedReader(
              new InputStreamReader(new FileInputStream(inFN),
                      Charset.forName("UTF-8")));

      while (reader.ready()) {
        line = reader.readLine().trim();

        //skip blank lines
        if (line.isEmpty()) {
          continue;
        }

        //skip the comment lines
        if (line.charAt(0) != commentChar) {
          tempArr.add(line);
        }

      } //while

      reader.close();

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
    }

    return tempArr;

  } //method


  private static void testreadPatterns() {
    String inFN = "pattern.txt";
    char commentChar = '\'';
    List<String> patStrArr = fileToArrayListSkipCommentLines(inFN, commentChar);
    GenUtil.showArrayList(patStrArr);

  } //method


  /**
   * Read a given file and put each line in a hash map. Each line contains the value and the key
   * would be the order of the value in the file.
   *
   * @param inFN - the given file name
   *
   * @return the hash map
   */
  public static Map<String, Integer> fileToHashMap(String inFN) {

    Map<String, Integer> tempHM = new HashMap<>();
    int i = 0;

    try {
      try (BufferedReader reader = new BufferedReader(new FileReader(new File(inFN)))) {

        while (reader.ready()) {
          tempHM.put(reader.readLine().trim(), i++);
        }
      }
      return tempHM;

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
      return null;
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
      return null;
    }

  } //method


  /**
   * read a given file and put each line in a hash map. the keys and values are separated by comma
   * in the file.
   *
   * @param inFN - the given file name
   *
   * @return the hash map containing the lines of the given file
   */
  public static Map<String, Integer> fileKeyCommaValueToHashMap(String inFN) {

    Map<String, Integer> tempHM = new HashMap<>();

    try {
      try (BufferedReader reader = new BufferedReader(new FileReader(new File(inFN)))) {

        while (reader.ready()) {
          String[] keyValueArr = reader.readLine().trim().split("[,]");
          tempHM.put(keyValueArr[0], Integer.parseInt(keyValueArr[1]));
        }
      }
      return tempHM;

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
      return null;
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
      return null;
    }

  } //method


  private static void testfileStringCodedToHashMap() {

    String functionWordsFN = ".\\FunctionWords\\functionWords.txt";

    Map<String, Integer> funcWordHM = fileKeyCommaValueToHashMap(functionWordsFN);
    GenUtil.showMap(funcWordHM);

  } //test method


  private static void cleanUpFunctionWords() {

    String functionWordsFN = ".\\FunctionWords\\Rachel\\functionWords.txt";
    List<String> arr = FileUtil.fileToArrayList(functionWordsFN);
    Collections.sort(arr);

        //GenUtil.showArrayList(arr);
    String outFN = ".\\FunctionWords\\Rachel\\functionWords2.txt";
    FileUtil.arrayListToFile(arr, outFN);

  } //test method


  /**
   * write an array list of strings to the given file.
   *
   * @param arr   - the given array list
   * @param outFN - the output file name
   */
  public static void arrayListToFile(List<String> arr, String outFN) {

    String str = StringUtil.arrayListToString(arr);
    FileUtil.stringToFile(str, outFN);

  } //method


  private static void testarrayListToFile() {
    String inFN = "testPattern.txt";
    List<String> arr = fileToArrayList(inFN);
    GenUtil.showArrayList(arr);

    String outFN = "testPatternOut.txt";

    arrayListToFile(arr, outFN);

  } //test method


  /**
   * read the given file and write the odd and even paragraphs into separate files. Pre-condition:
   * the file is cleaned up and paragraphs are separated.
   *
   * @param inpFN  - the given input file name
   * @param locale - the input file locale
   * @param oddFN  - the odd file name
   * @param evenFN - the even file name
   */
  public static void separateFileIntoOddEvenParagraph(
          String inpFN, Locale locale, String oddFN, String evenFN) {

    String inpStr = fileToString(inpFN);
        //Test: System.out.println("inpStr = " + inpStr);

//        ArrayList<String> sentArr =
//            StringUtil.stringToParagraphArr(inpStr, locale);
//
//        ArrayList<String> oddSentArr = new ArrayList();
//        ArrayList<String> evenSentArr = new ArrayList();
//
//        GenUtil.separateArrToOddEvenArr(sentArr, oddSentArr, evenSentArr);
//
//        FileUtil.arrayListToFile(oddSentArr, oddFN);
//        FileUtil.arrayListToFile(evenSentArr, evenFN);
  } //method


  private static void testseparateFileIntoOddEvenParagraph() {
    Locale locale = new Locale("en_US");

    String inpFN = ".\\Test\\Mark Twain - Adventures of Huckleberry Finn.txt";
    String oddFN = ".\\TestClean\\Mark Twain - Adventures of Huckleberry Finn_odd_PARAG.txt";
    String evenFN = ".\\TestClean\\Mark Twain - Adventures of Huckleberry Finn_even_PARAG.txt";

    separateFileIntoOddEvenParagraph(inpFN, locale, oddFN, evenFN);

  } //method


  /**
   * read the given file and write the odd and even sentences into separate files.
   *
   * @param inpFN  - the given input file name
   * @param locale - the input file locale
   * @param oddFN  - the odd file name
   * @param evenFN - the even file name
   */
  public static void separateFileIntoOddEvenSentences(
          String inpFN, Locale locale, String oddFN, String evenFN) {

    String inpStr = fileToString(inpFN);
        //Test: System.out.println("inpStr = " + inpStr);

//        List<String> sentArr =
//            StringUtil.stringToSentenceArr(inpStr, locale);
//
//        List<String> oddSentArr = new ArrayList();
//        List<String> evenSentArr = new ArrayList();
//
//        GenUtil.separateArrToOddEvenArr(sentArr, oddSentArr, evenSentArr);
//
//        FileUtil.arrayListToFile(oddSentArr, oddFN);
//        FileUtil.arrayListToFile(evenSentArr, evenFN);
  } //method


  private static void testseparateFileIntoOddEvenSentences() {
    Locale locale = new Locale("en_US");

    String inpFN = ".\\Test\\Mark Twain - Adventures of Huckleberry Finn.txt";
    String oddFN = ".\\TestClean\\Mark Twain - Adventures of Huckleberry Finn_odd_SENT.txt";
    String evenFN = ".\\TestClean\\Mark Twain - Adventures of Huckleberry Finn_even_SENT.txt";

    separateFileIntoOddEvenSentences(inpFN, locale, oddFN, evenFN);

  } //method


  /**
   * Read the given file and convert it into a string but sense the paragraphs and return the
   * string.
   *
   * @param inFN - the given input file name
   *
   * @return the files as a string
   */
  public static String fileToStringSenseParagraphRemoveBlankLines(String inFN) {
    StringBuilder streamSB = new StringBuilder();
    BufferedReader reader;

    StringBuilder parSB = new StringBuilder();

    try {
      reader = new BufferedReader(
              new InputStreamReader(new FileInputStream(inFN),
                      Charset.forName("UTF-8")));

      while (reader.ready()) {

        String line = reader.readLine().trim();

        if (line.length() == 0) {
          if (parSB.toString().length() == 0) {
            continue;

          } else {
            parSB.append(line).append("\r\n");
            streamSB.append(parSB.toString());
            parSB = new StringBuilder();
          }

        } else {
          parSB.append(line).append(" ");
        }

      } //while

      if (parSB.toString().length() != 0) {
        streamSB.append(parSB.toString()).append("\r\n");
      }

      reader.close();

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
    }

    return streamSB.toString();

  } //method


  /**
   * Divide a file into two halves.
   *
   * @param inFN - the given input file name
   *
   * @return the number of chars of the file
   */
  public static void divideFileIntoHalf(
          String inFN, String outFN1, String outFN2) {
    String fileStr = fileToString(inFN);
    int half = fileStr.length() / 2;
    int breakPoint = fileStr.indexOf('\r', half) + 2; //to include \r\n
    String halfOne = fileStr.substring(0, breakPoint);
    String halfTwo = fileStr.substring(breakPoint);

    FileUtil.stringToFile(halfOne, outFN1);
    FileUtil.stringToFile(halfTwo, outFN2);

  } //method


  private static void testdivideFileIntoHalf() {
    String inFN = ".\\Text\\test.txt";
    String outFN1 = ".\\Text\\test_HALF_ONE.txt";
    String outFN2 = ".\\Text\\test_HALF_TWO.txt";

    FileUtil.divideFileIntoHalf(inFN, outFN1, outFN2);

  } //test method


  /**
   * Divide a file into two (almost) halves and return an array list of them and add the input
   * string to the array list as well.
   *
   * @param inFN - the given input file name
   *
   * @return the number of chars of the file
   */
  public static List<String> divideFileIntoTwoHalfStrPlus(String inFN) {
    String fileStr = fileToString(inFN);
    return StringUtil.stringToTwoHalvesPlus(fileStr);

  } //method


  /**
   * Divide a file into two (almost) halves and return an array list of them and add the input
   * string to the array list as well.
   *
   * @param inFN - the given input file name
   *
   * @return the number of chars of the file
   */
  public static List<String> divideFileIntoTwoHalfStr(String inFN) {
    String fileStr = fileToString(inFN);
    return StringUtil.stringToTwoHalves(fileStr);

  } //method


  /**
   * Read the given file and convert it into a string but remove the blank lines.
   *
   * @param inFN - the given input file name
   *
   * @return the files as a string
   */
  public static String fileToStringRemoveBlankLines(String inFN) {
    StringBuilder streamSB = new StringBuilder();
    BufferedReader reader;

    try {
      reader = new BufferedReader(
              new InputStreamReader(new FileInputStream(inFN),
                      Charset.forName("UTF-8")));

      while (reader.ready()) {

        String line = reader.readLine().trim();

        if (line.length() != 0) {
          streamSB.append(line).append("\r\n");
        }

      } //while

      reader.close();

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
    }

    return streamSB.toString();

  } //method


  /**
   * Remove blank line of a given file.
   *
   * @param inFN  - the given input file name
   * @param outFN - the given output file name
   */
  public static void fileRemoveBlankLine(String inFN, String outFN) {

    String str = FileUtil.fileToStringRemoveBlankLines(inFN);
    FileUtil.stringToFile(str, outFN);

  } //method


  /**
   * Clean up all files of a given folder and write them in the given folder.
   *
   * @param inFolder  - the given input folder
   * @param outFolder - the given output folder
   *
   * @return the files as string
   */
  public static void folderRemoveBlankLines(String inFolder,
                                            String outFolder) {

    List<String> filesArr = getFolderFNArr(inFolder);

    for (String fileName : filesArr) {

      System.out.println("Removing blank lines of file: " + fileName);

      FileUtil.fileRemoveBlankLine(
              inFolder + fileName, outFolder + fileName);
    } //for

  } //method


  private static void testfolderRemoveBlankLines() {
    String inFolder = ".\\Zhang Books\\";
    String outFolder = ".\\Zhang Books-CNB\\";

    folderRemoveBlankLines(inFolder, outFolder);

  } //method


  /**
   * Create a folder with the given name. Pre-condition: the folder name doesn't contain sub-folder.
   * Pre-condition: the folder exists and is really a folder.
   *
   * @param folderName - the given folder name including the path
   */
  public static void createFolder(String folderName) {

    File dir = new File(folderName);
    dir.mkdir();

  } //method


  /**
   * Copy a file with the given name to somewhere else. Pre-condition: the folder name doesn't
   * contain sub-folder. Pre-condition: the folder exists and is really a folder.
   *
   * @param folderName - the given folder name including the path
   */
  public static void copyFile(String fileName, String folderName) {

    String fileStr = FileUtil.fileToString(fileName);
    FileUtil.stringToFile(fileStr, folderName);

  } //method


  /**
   * Delete a folder and its contents. Pre-condition: the folder doesn't contain sub-folder.
   *
   * @param folderName - the given folder name including the path
   *
   * @return true if the operation is successful and false otherwise.
   */
  public static void deleteFolder(String folderName) {

    File folder = new File(folderName);
    if (!folder.exists() || !folder.isDirectory()) {
      return;
    }
    File[] filesArr = folder.listFiles();

    for (File file : filesArr) {
      file.delete();
    }

    folder.delete();

  } //method


  /**
   * Delete a the contents (files) of a folder. Pre-condition: the folder doesn't contain
   * sub-folder.
   *
   * @param folderName - the given folder name including the path
   *
   * @return true if the operation is successful and false otherwise.
   */
  public static void deleteFolderContent(String folderName) {

    File folder = new File(folderName);

    if (!folder.exists() || !folder.isDirectory()) {
      return;
    }
    File[] filesArr = folder.listFiles();

    for (File file : filesArr) {
      file.delete();
    }

  } //method


  private static void testgetFolderFiles() {
    String filesPath = ".\\test\\";

    List<String> filePathArr = getFolderFNArr(filesPath);
    GenUtil.showArrayList(filePathArr);

  } //test method


  /**
   * Writes a give string to a given file using UTF-8 encoding.
   *
   * @param str   - the given string
   * @param outFN - the give output file name
   */
  public static void stringToFile(String str, String outFN) {

    BufferedWriter writer;

    try {
      writer = new BufferedWriter(
              new OutputStreamWriter(
                      new FileOutputStream(outFN), StandardCharsets.UTF_8));

      writer.write(str);
      writer.close();

    } catch (IOException ex) {
      ex.printStackTrace();
    }

  } //method


  private static void teststringToFile() {
    String inFN = ".\\Test\\MOBY DICK.txt";
    String str = FileUtil.fileToString(inFN);

    String outFN = ".\\Test\\MOBY DICK_O.txt";
    FileUtil.stringToFile(str, outFN);

  } //test method


  private static void test() {
    File testFN = new File(".\\Temp\\HP1.txt");

    String fileStr = FileUtil.fileToString(testFN.getPath());
    FileUtil.stringToFile(fileStr, ".\\MyTemp\\");

  } //test method


  /**
   * Read a given file and put each line in a hash map. The lines contain the stop words, and the
   * function word codes. This method ignore the given code and put the line number as the value
   * instead.
   *
   * @param inFN - the given file name
   *
   * @return the hash map containing the lines of the given file
   */
  public static Map<String, Integer> fileToHashMapLineNumberAsKey(
          String inFN) {

    Map<String, Integer> tempHM = new HashMap<>();

    try {
      try (BufferedReader reader = new BufferedReader(new FileReader(new File(inFN)))) {
        Integer val = 1;

        while (reader.ready()) {
          String[] keyValueArr = reader.readLine().trim().split("[,]");
          tempHM.put(keyValueArr[0], val++);
        }
      }
      return tempHM;

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
      return null;
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
      return null;
    }

  } //method


  private static void testfileToHashMapLineNumberAsKey() {
    String functionWordsFN = ".\\FunctionWords\\functionWords.txt";

    Map<String, Integer> funcWordHM = fileToHashMapLineNumberAsKey(functionWordsFN);
    GenUtil.showMap(funcWordHM);

  } //test method


  /**
   * read the given file and return it as a string.
   *
   * @param inFN - the given input file name
   *
   * @return the files as string
   */
  public static String fileToString(String inFN) {
    StringBuilder streamSB = new StringBuilder();
    BufferedReader reader;

    try {
      reader = new BufferedReader(
              new InputStreamReader(new FileInputStream(inFN),
                      Charset.forName("UTF-8")));

      while (reader.ready()) {

        String line = reader.readLine().trim();

        if (line.length() == 0) {
          streamSB.append("\r\n");
        } else {
          streamSB.append(line).append("\r\n");
        }

      } //while

      reader.close();

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
    }

    return streamSB.toString();

  } //method


  private static void testfileToString() {

    String inFN = ".\\SimilarSentence\\learn.txt";

    String tempStr = FileUtil.fileToString(inFN);
    System.out.println("*****  after file to string **** \n[" + tempStr + "]");

  } //test method


  /**
   * Return all files names of a given folder name. Note that the file name does not contain the
   * path.
   *
   * @param folderName - the given folder full name
   *
   * @return the array list of the files names
   */
  public static List<String> getFolderFNArr(String folderName) {

    File folder = new File(folderName);
    File[] filesArr = folder.listFiles();

    List<String> fileNameArr = new ArrayList<>();

    for (File file : filesArr) {
      fileNameArr.add(file.getName()); //no path
    }
    return fileNameArr;

  } //method


  /**
   * Read a given file line by line and get the first item and discard the rest. The items are
   * separated by comma.
   *
   * @param inFN - the given file name
   *
   * @return the hash map containing the lines of the given file
   */
  public static List<String> getFirstItems(String inFN) {

    List<String> tempArr = new ArrayList<>();

    try {
      try (BufferedReader reader = new BufferedReader(new FileReader(new File(inFN)))) {

        while (reader.ready()) {
          tempArr.add(reader.readLine().trim().split("[,]")[0]);
        }

      } //try

      return tempArr;

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
      return null;
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
      return null;
    }

  } //method


  /**
   * This method also is used to create the raw function words.
   */
  private static void getRawFunctionWords() {

    String functionWordsFN = ".\\FunctionWords\\functionWords.txt";
    String functionWordsRawFN = ".\\FunctionWords\\functionWordsRaw.txt";

    List<String> tempArr = getFirstItems(functionWordsFN);

    GenUtil.showArrayList(tempArr);

    arrayListToFile(tempArr, functionWordsRawFN);

  } //test method


  /**
   * Read a given file line by line and get the first item and discard the rest. The items are
   * separated by comma.
   *
   * @param inFN - the given file name
   *
   * @return the hash map containing the lines of the given file
   */
  public static void getRawFunctionWordsCategorized(String inFN) {

    List<String> tempArr1 = new ArrayList<>(); //Auxiliary verb
    List<String> tempArr2 = new ArrayList<>(); //Conjunction
    List<String> tempArr3 = new ArrayList<>(); //Determiner
    List<String> tempArr4 = new ArrayList<>(); //Preposition
    List<String> tempArr5 = new ArrayList<>(); //Pronoun
    List<String> tempArr6 = new ArrayList<>(); //Quantifier
    String line, functionWord;
    int cat;

    try {
      try (BufferedReader reader = new BufferedReader(new FileReader(new File(inFN)))) {

        while (reader.ready()) {

          line = reader.readLine().trim();
          String[] itemsArr = line.split("[,]");
          functionWord = itemsArr[0];
          cat = Integer.valueOf(itemsArr[1]);
          switch (cat) {
            case 1:
              tempArr1.add(functionWord);
              break;

            case 2:
              tempArr2.add(functionWord);
              break;

            case 3:
              tempArr3.add(functionWord);
              break;

            case 4:
              tempArr4.add(functionWord);
              break;

            case 5:
              tempArr5.add(functionWord);
              break;

            case 6:
              tempArr6.add(functionWord);
              break;

          } //switch

        } //while
      } //try

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
    }

    GenUtil.showBareArrayList(tempArr1, "Auxiliary verb");
    GenUtil.showBareArrayList(tempArr2, "Conjunction");
    GenUtil.showBareArrayList(tempArr3, "Determiner");
    GenUtil.showBareArrayList(tempArr4, "Preposition");
    GenUtil.showBareArrayList(tempArr5, "Pronoun");
    GenUtil.showBareArrayList(tempArr6, "Quantifier");

  } //method


  /**
   * This method also is used to create the raw function words categorized.
   */
  private static void testgetRawFunctionWordsCategorized() {

    String functionWordsFN = ".\\FunctionWords\\functionWords.txt";
    getRawFunctionWordsCategorized(functionWordsFN);

  } //test method


  /**
   * This method is used to get the list of e-books used in this thesis.
   */
  private static void getEBooksList() {

    String folderName = ".\\TestCaseSet02\\testCase04\\";
    List<String> tempArr = getFolderFNArr(folderName);
    System.out.println();

    for (int i = 1; i <= tempArr.size(); i++) {
      System.out.format("%s%n", tempArr.get(i - 1).toString());
    }

    System.out.println();

  } //method


  /**
   * Read a given file line by line and create a matrix of data and then change the row and columns
   * order and write in the file
   *
   * @param inFN  - the given input file name
   * @param outFN - the given output file name
   */
  public static String[][] getMatrix(String inFN, int rows) {

    String line;
    String[][] matrix = new String[rows][];
    int index = 0;

    try {
      try (BufferedReader reader = new BufferedReader(new FileReader(new File(inFN)))) {

        while (reader.ready()) {

          line = reader.readLine().trim();
          matrix[index++] = line.split("\\p{javaWhitespace}+");

        } //while
      } //try

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
    }

    return matrix;

  } //method


  private static void testgetMatrix() {

    String inFN = ".\\TestCaseSet02\\test\\test.txt";
    int rows = 9;
    String[][] matrix = getMatrix(inFN, rows);
    int length = 8;
    GenUtil.showStringDoubleArray(matrix, length, "hey");

  } //test method


  /**
   * Read a given file line by line and create a matrix of data and then change the row and columns
   * order and write in the file
   *
   * @param inFN  - the given input file name
   * @param outFN - the given output file name
   */
  public static String[][] transposeMatrix(String[][] inMatrix) {

    int row = inMatrix.length;
    int col = inMatrix[0].length;
    String[][] outMatrix = new String[col][row];

    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        outMatrix[j][i] = inMatrix[i][j];
      }
    }

    return outMatrix;

  } //method


  private static void testtransposeMatrix() {

    String inFN = ".\\TestCaseSet02\\test\\test 3.txt";
    int rows = 10;
    String[][] inMatrix = getMatrix(inFN, rows);
    int length = 8;
    GenUtil.showStringDoubleArray(inMatrix, length, "hey");

    String[][] outMatrix = transposeMatrix(inMatrix);
    GenUtil.showStringDoubleArray(outMatrix, length, "hey");

  } //test method


  /**
   * Reads the given file and return it as an array list of Verses.
   *
   * @param inFN - the given input file name
   *
   * @return the array list of Verses.
   */
  public static List<Verse> fileToVerse(String inFN) {
    BufferedReader reader;
    List<Verse> arrLst = new ArrayList<>(Const.VERSE_NUM);

    try {
      reader = new BufferedReader(new InputStreamReader(
              new FileInputStream(inFN), Charset.forName("UTF-8")));

      int index = 0;

      while (reader.ready()) {

        String line = reader.readLine().trim();
        String[] tempLst = line.split(Const.DELEMETER);
//                System.out.println("tempLst[0] = {" + tempLst[0] + "}");
//                System.out.println("tempLst[1] = {" + tempLst[1] + "}");
//                System.out.println("tempLst[2] = {" + tempLst[2] + "}");

        int chapter = Integer.parseInt(tempLst[0]);
        int verse = Integer.parseInt(tempLst[1]);
        Verse tempVerse = new Verse(chapter, verse, tempLst[2]);

        arrLst.add(tempVerse);

        if (index++ > 10) {
          break;
        }

      } //while

      reader.close();

    } catch (FileNotFoundException ex) {
      System.err.println(ex.getMessage());
    } catch (IOException ex) {
      System.err.println(ex.getMessage());
    }

    return arrLst;

  } //method


  private static void testfileToVerse() {

    //String inFN  = ".\\translation\\fa.fooladvand.txt";
    String inFN = ".\\quran\\quran.txt";

    List<Verse> arrLst = FileUtil.fileToVerse(inFN);

    GenUtil.showArrayList(arrLst);
    //System.out.println("*****  after file to string **** \n[" + tempStr + "]");

  } //test method


  /**
   * Give the files names of a given folder.
   *
   * @param inFolder - the given input folder
   *
   * @return the list containing the folders file name and full path.
   */
  public static List<String> folderToArrayList(String inFolder) {

    List<String> unitArr = new ArrayList<>();
    List<String> filesArr = FileUtil.getFolderFNPlusPathArr(inFolder);

    for (String fileName : filesArr) {

      unitArr.add(FileUtil.fileToString(fileName));

    } //for

    return unitArr;

  } //method


  private static void testfolderToArrayList() {
    String inFolder = ".\\translation\\";

    List<String> unitArr = folderToArrayList(inFolder);
    GenUtil.showArrayList(unitArr);

  } //method


  /**
   * read a folder files names + path from the given path.
   *
   * @param folderName - the given folder name including path
   *
   * @return the array list of the files names
   */
  public static List<String> getFolderFNPlusPathArr(String folderName) {

    File folder = new File(folderName);
    File[] filesArr = folder.listFiles();

    List<String> fileNameArr = new ArrayList<>();

    for (File file : filesArr) {
      fileNameArr.add(file.getPath());
    } //for

    return fileNameArr;

  } //method


  private static void testgetFolderFilesIncludePath() {
    String filesPath = "translation\\";

    List<String> filesArr = getFolderFNPlusPathArr(filesPath);
    GenUtil.showArrayList(filesArr);

//        ArrayList<String> filePathArr = getFolderFNArr(filesPath);
//        GenUtil.showArrayList(filePathArr);
  } //test method


  /**
   */
// ........................ M A I N   M E T H O D ............................//
  /**
   * This main method is just for testing this class.
   *
   * @param args - the arguments
   */
  public static void main(String[] args) {

    /* ....................................................*/
    testgetFolderFilesIncludePath();
    /* ....................................................*/

  } //main method

} //class

